package com.sbtr.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.system.dto.HelpInfoTreeDto;
import com.sbtr.business.system.entity.HelpParam;
import com.sbtr.business.system.service.HelpParamService;
import com.sbtr.common.Response;
import com.sbtr.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.sbtr.common.PageDto;
import com.sbtr.business.system.entity.HelpInfo;
import com.sbtr.business.system.dto.HelpInfoDto;
import com.sbtr.business.system.service.HelpInfoService;
import org.springframework.web.bind.annotation.RestController;
import com.sbtr.controller.common.CommonController;

import java.util.List;

/**
 * 帮助说明基本信息字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-07-18
 */
@RestController
@RequestMapping("/helpInfo")
@Api(tags = "帮助说明基本信息字段列表")
public class HelpInfoController extends CommonController<HelpInfo,HelpInfoService> {

    @Autowired
    HelpParamService helpParamService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页列表")
    public Response<PageDto<HelpInfo>> pageList(HelpInfoDto helpInfoDto){
        return Response.success(u.pageList(helpInfoDto));
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "保存")
    public Response<HelpInfo> save(@RequestBody HelpInfo helpInfo){
        helpInfo.setId(null);
        if(helpInfo.insert()){
            return Response.success(helpInfo);
        }else throw new BDException("保存失败");
    }


    @GetMapping("/getTree")
    @ApiOperation(httpMethod = "GET", value = "获取帮助信息总汇")
    public Response<HelpInfoTreeDto> getTree(String helpCode){
        QueryWrapper<HelpInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("help_code",helpCode);
        queryWrapper.last("limit 1");
        HelpInfo helpInfo = u.getOne(queryWrapper);
        HelpInfoTreeDto helpInfoDto = BeanCopyUtils.copyProperties(helpInfo, HelpInfoTreeDto.class);
        QueryWrapper<HelpParam> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("help_id",helpInfo.getId());
        List<HelpParam> helpParams=helpParamService.list(queryWrapper2);
        helpInfoDto.setHelpParam(helpParams);
        return Response.success(helpInfoDto);
    }




}

